// scaup
// (c) 2010, Normen Müller, normen.mueller@gmail.com  
// $Id: URIs.scala 97 2010-08-23 09:00:50Z normen.mueller $
package scaup.data

import java.net.{URI, URLEncoder, URLDecoder}

import scaup.PimpedType

/** @author  <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.6
 */
sealed trait URIW extends PimpedType[URI] {
  import scaup.Scaup._

  lazy val utf8enc = URLEncoder.encode(value.toString(), "UTF-8")

  lazy val utf8dec = URLDecoder.decode(value.toString(), "UTF-8")

  def /(child: String) = new URI(value.getScheme, value.getUserInfo, value.getHost, value.getPort, {
    if (value.getPath endsWith "/")
      if (child startsWith "/") value.getPath + child substring 1
      else value.getPath + child
    else if (child startsWith "/") value.getPath + child
    else value.getPath + "/" + child
  }, value.getQuery, value.getFragment)
}

/** @author <a href="mailto:normen.mueller@googlemail.com">Normen M&#xFC;ller</a>
 *  @version 0.0.6
 */
trait URIs {
  implicit def URITo(uri: URI): URIW = new URIW {
    val value = uri
  }
}
